﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using DynamicComparer;
using Shuba.Model;

namespace Shuba.Web.master
{
    public partial class negative_list : basePage
    {
        string msg = "";
        bool IsView, IsAdd, IsEdit, IsDelete, IsChecker;

        protected void Page_Load(object sender, EventArgs e)
        {
            Form.DefaultFocus = txtSearch.ClientID;
            Form.DefaultButton = btnSearch.UniqueID;

            string acl = GetUserAccess("MSTR_NEGATIVE_FILE");
            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsView)
            {
                msg = GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitSort();
                BindData();

                LabelTitle.Text = GetPageTitle("MSTR_NEGATIVE_FILE");

                if (IsAdd) btnUpload.Enabled = true; else btnUpload.Enabled = false;
            }
        }

        private void BindData()
        {
            Shuba.Business.NegativeFile BI = new Shuba.Business.NegativeFile();
            List<NegativeFileInfo> list = BI.GetList(txtSearch.Text);

            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<NegativeFileInfo> comparer = new DynamicComparer<NegativeFileInfo>(sort);
                list.Sort(comparer);
            }
            gvList.DataSource = list;
            gvList.DataBind();

            //FOR APPROVAL GRID
            if (IsChecker)
            {
                BindApprovalData();
                if (ApprovalGrid.Rows.Count > 0) { ApprovalPnl.Visible = true; } else { ApprovalPnl.Visible = false; }
            }
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;
            BindData();
        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }


        /*======================================
               APPROVAL GRID START HERE
        =======================================
        */
        private void BindApprovalData()
        {
            Shuba.Business.NegativeFile BI = new Shuba.Business.NegativeFile();
            List<NegativeFileInfo> list = BI.GetListApproval("", HttpContext.Current.User.Identity.Name);

            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<NegativeFileInfo> comparer = new DynamicComparer<NegativeFileInfo>(sort);
                list.Sort(comparer);
            }
            ApprovalGrid.DataSource = list;
            ApprovalGrid.DataBind();
        }

        protected void ApprovalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ApprovalGrid.PageIndex = e.NewPageIndex;
            BindApprovalData();
        }

        protected void ApprovalGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            ApprovalGrid.PageIndex = 0;
            BindApprovalData();
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");

                if (chkItemSelect.Checked)
                {
                    string ROW_ID = ApprovalGrid.DataKeys[row.RowIndex].Value.ToString(); 

                    Approve(ROW_ID, true);
                }
            }
            msg = GetGlobalResourceObject("Messages", "ApproveAlert").ToString();
            BindData();
            Alert(msg);

            //AlertAndRedirect(msg, Request.Url.PathAndQuery);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");

                if (chkItemSelect.Checked)
                {
                    string ROW_ID = ApprovalGrid.DataKeys[row.RowIndex].Value.ToString();

                    Approve(ROW_ID, false);
                }
            }
            msg = GetGlobalResourceObject("Messages", "NoApprove").ToString();
            BindData();
            Alert(msg); 
            
            //AlertAndRedirect(msg, Request.Url.PathAndQuery);
        }

        private int Approve(string ROW_ID, bool IsApprove)
        {
            int status = 0;
            Shuba.Business.NegativeFile BI = new Shuba.Business.NegativeFile();
            Shuba.Model.NegativeFileInfo BIInfo = new Shuba.Model.NegativeFileInfo();

            BIInfo = BI.GetApprovalByID(ROW_ID);
            BIInfo.UPDATE_BY = HttpContext.Current.User.Identity.Name;
            BIInfo.UPDATE_DATE = ProcessingDate();

            if (IsApprove)
            {
                try
                {
                    AddLog("APPROVAL", "APPROVE NEGATIVE FILE");
                    status = BI.Approve(ROW_ID);
                }
                catch (Exception ex)
                {
                    ErrBox.Visible = true;
                    ErrMsg.Text = ex.Message;
                }
            }
            else
            {
                AddLog("APPROVAL", "REJECT NEGATIVE FILE");
                status = BI.Reject(ROW_ID);
            }
            return status;
        }

        protected void btnApproveAll_Click(object sender, EventArgs e)
        {
            int ErrorCount = 0, SuccessCount = 0;

            Shuba.Business.NegativeFile BI = new Shuba.Business.NegativeFile();
            List<NegativeFileInfo> list = BI.GetListApproval("", HttpContext.Current.User.Identity.Name);

            foreach (NegativeFileInfo item in list)
            {
                try
                {
                    if (Approve(item.ROW_ID.ToString(), true) > 0)
                    {
                        SuccessCount++;
                    }
                    else
                    {
                        ErrorCount++;
                    }
                }
                catch (Exception)
                {
                    ErrorCount++;
                }
            }
            if (SuccessCount > 0 || ErrorCount > 0)
            {
                msg = "Approval Notification\\n\\nTotal record to approve : " + list.Count + "Record(s)\\nSuccessfully Approve : " + SuccessCount + " Record(s)\\nFailed to Approve : " + ErrorCount + " Record(s)";
            }
            BindData();
            Alert(msg);
        }

        protected void btnRejectAll_Click(object sender, EventArgs e)
        {
            int ErrorCount = 0, SuccessCount = 0;

            Shuba.Business.NegativeFile BI = new Shuba.Business.NegativeFile();
            List<NegativeFileInfo> list = BI.GetListApproval("", HttpContext.Current.User.Identity.Name);

            foreach (NegativeFileInfo item in list)
            {
                try
                {
                    if (Approve(item.ROW_ID.ToString(), false) > 0)
                    {
                        SuccessCount++;
                    }
                    else
                    {
                        ErrorCount++;
                    }
                }
                catch (Exception)
                {
                    ErrorCount++;
                }
            }
            if (SuccessCount > 0 || ErrorCount > 0)
            {
                msg = "Approval Notification\\n\\nTotal record to reject : " + list.Count + "Record(s)\\nSuccessfully Reject : " + SuccessCount + " Record(s)\\nFailed to Reject : " + ErrorCount + " Record(s)";
            }
            BindData();
            Alert(msg);
        }
    }
}
